<?php
/*
For safety: 
	//For 40
	$a_members = db_selectcol_array($dbh_r, 'select id from member_imore 	where last_expert_approval_type<> expert_approval and status=1 and (NOW()- INTERVAL 5 MINUTE > lastmodify)');
	
	//For 34
	$a_members = db_selectcol_array($dbh_r, 'select id from member_imore 	where email<>"" and last_expert_approval_type<> expert_approval and status=1 and (NOW()- INTERVAL 5 MINUTE > lastmodify)');
	
	
member_type	->1
expert apprvoal ->3


every time


member_type

試用
update member_imore set expert_approval_type=3 where and expert_approval_type<3 and id=?
update member_imore set  member_type =1 where member_type<1 and id=?;

體驗團 
update member_imore set expert_approval_type=4 where expert_approval_type<=5 and id=?



4-5, 4-6, 

6-4

last_expert_approval_status
*/



require_once('/home/itrial/www/appconf.php');
require_once "CMS.class.php";

$local_config = array(
    'this_cgi'       => 'crontab/send_card.php',
    'tmpl'           => array('base_path' => 'crontab/member/email_notification/'), # full path = $this->param('TMPL').$
);

class backend_crontab_member_email_notification extends CMS{

  function setup(){
    $this->run_modes('send');
    $this->start_mode('send');
    $this->set_path_info_position(array('id'=>1));
    parent::setup();
  }


#no no login

	function cgiapp_prerun(){
	}
  function action_send(){
  	

  	$dbh_r = $this->db_connect('read');
  	$dbh_w = $this->db_connect('write');
  	
	// Process request 30mins after the confirmation
	//For 40
  	//$a_members = db_selectcol_array($dbh_r, 'select id from member_imore 	where last_expert_approval_type<> expert_approval and status=1 and (NOW()- INTERVAL 5 MINUTE > lastmodify)');
	//For 34
	$a_members = db_selectcol_array($dbh_r, 'select id from member_imore where email<>"" and last_expert_approval_type<> expert_approval and status=1 and (NOW()- INTERVAL 5 MINUTE > lastmodify)');
	  //$a_members = db_selectcol_array($dbh_r, 'select id from member_imore where id=6993');

  	foreach ($a_members as $member){
      echo "Processing ID: $member\n";
  		$this->process_request($dbh_w, $dbh_r, $member);
  	}
  	echo "Done\n";
	  return "OK";	  
	  
  }
  
  
  private function update_approval_status($dbh_w, $member, $new_statsus){
  	db_do($dbh_w, 'update member_imore set last_expert_approval_type=? where id=?', array($new_statsus, $member));
  }
  
  
  private function log_mail($type, $mid,$email){
  	$dbh_w = $this->db_connect('write');
  	db_do($dbh_w, 'insert into member_email_log (type, mid, email,addtime,lastmodify) values (?,?,?,NOW(), NOW())', array($type, $mid,$email));
  }
  
  private function email_became_new_member($dbh_r, $member){
  	list ($username, $user_email) = db_selectrow_array($dbh_r, 'select username, email from member_imore where id=?', $member);
    $subject = '成功登記成為iMORE.hk 會員之確認信';
    
    $this->tmpl_assign('username', $username);    
    $this->tmpl_assign('email_activation_link', $this->email_activation_content($member));
    
    $body = $this->load_action_tmpl('became_new_member');
    
    #echo "email_became_new_member to $user_email [$member]";
    $this->log_mail('became_new_member', $member, $user_email);
    
    $mailer = getModel('Model_CMS_Mailer');
    $mailer->email_from_webmaster($subject, $body, $user_email, $username);	
    
  }
  
  
  // Email C in doc
  private function email_became_trialist($dbh_r, $member){
  	list ($username, $user_email) = db_selectrow_array($dbh_r, 'select username, email from member_imore where id=?', $member);
    $subject = 'iTrial by iMORE 會員已成功申請成為試用專員之確認信 (尚未申請成為體驗達人)';
    
    $this->tmpl_assign('username', $username);    
    $this->tmpl_assign('email_activation_link', $this->email_activation_content($member));
    $body = $this->load_action_tmpl('became_trialist');
    
    #echo "became_trailist to $user_email [$member]";
    $this->log_mail('became_trailist', $member, $user_email);

    
    $mailer = getModel('Model_CMS_Mailer');
    $mailer->email_from_webmaster($subject, $body, $user_email, $username);	  	
  }
  
  
  // mail D - apply all in once step
  private function email_complete_apply_as_expert_once($dbh_r, $member){
  	list ($username, $user_email) = db_selectrow_array($dbh_r, 'select username, email from member_imore where id=?', $member);
    $subject = 'iTrial by iMORE 會員已一次性完成申請成為 (會員、試用達人及尊業體驗團之成員) 之通知';
    
    $this->tmpl_assign('username', $username);    
    $this->tmpl_assign('email_activation_link', $this->email_activation_content($member));
    $body = $this->load_action_tmpl('complete_apply_as_expert_once');
    
    #echo "complete_apply_as_expert_once to $user_email [$member]";
    $this->log_mail('complete_apply_as_expert_once', $member, $user_email);

    
    $mailer = getModel('Model_CMS_Mailer');
    $mailer->email_from_webmaster($subject, $body, $user_email, $username);	  	
  }
  
  // Mail E1
  private function email_became_expert($dbh_r, $member){
  	list ($username, $user_email) = db_selectrow_array($dbh_r, 'select username, email from member_imore where id=?', $member);
    $subject = '通過審核成為 iTrial by iMORE 體驗達人之確認信';
    
    echo "E";
    $this->tmpl_assign('username', $username);    
    $this->tmpl_assign('email_activation_link', $this->email_activation_content($member));
    $body = $this->load_action_tmpl('became_expert');
    
    #echo "became_expert to $user_email [$member]";
    $this->log_mail('became_expert', $member, $user_email);
    
    $mailer = getModel('Model_CMS_Mailer');
    $mailer->email_from_webmaster($subject, $body, $user_email, $username);	  	
  }
  
  // Mail E2
  private function email_expert_application_failed($dbh_r, $member){
  	list ($username, $user_email, $reject_reason) = db_selectrow_array($dbh_r, 'select username, email,expert_reject_reason from member_imore where id=?', $member);
    $subject = '未能成功通過審核成為 iTrial by iMORE 體驗達人之通知';
    
    
     
    
    $this->tmpl_assign('username', $username);    
    $this->tmpl_assign('reject_reason', $reject_reason);  
    $this->tmpl_assign('email_activation_link', $this->email_activation_content($member));
    $body = $this->load_action_tmpl('expert_application_failed');
    
    #echo "expert_application_failed to $user_email [$member]";
    $this->log_mail('expert_application_failed', $member, $user_email);
    
    $mailer = getModel('Model_CMS_Mailer');
    $mailer->email_from_webmaster($subject, $body, $user_email, $username);	  	
  }
  
  private function email_activation_content($member){
    $emailActivator = getModel('Model_CMS_EmailActivation');
    return $emailActivator->email_content_html($member);
  }
  
  
  function process_request($dbh_w, $dbh_r, $member){

  	list ( $expert_approval, $last_expert_approval_type) = db_selectrow_array($dbh_r, 'select expert_approval, last_expert_approval_type from member_imore where id=?', $member);

	  echo "MID: $member. Expert_approval $expert_approval| Last: $last_expert_approval_type\n";
  	
/*	$subject = '';
	$body = '';
	$sender_email = '';
	$sender_name = '';*/
	
	
	if($expert_approval == 2 && $last_expert_approval_type < 2){
		//update last_expert_approval_type to 2
		//email b
		$this->email_became_new_member($dbh_r, $member);
		$this->update_approval_status($dbh_w, $member, 2);
		
		return;
	}  
	
	
	if($expert_approval == 3 && $last_expert_approval_type < 3){
		$msg = 'iMORE.hk 會員已成功申請成為試用專員之確認信 (尚未申請成為體驗達人)';
		//update last_expert_approval_type to 3
		//email c
		$this->email_became_trialist($dbh_r, $member);
		$this->update_approval_status($dbh_w, $member, 3);
		
	}else if($expert_approval == 4 && $last_expert_approval_type < 4){
		//email d
		$this->email_complete_apply_as_expert_once($dbh_r, $member);
		$this->update_approval_status($dbh_w, $member, 4);
		
	}else if($expert_approval == 5 && 
	   $last_expert_approval_type != 5  ){

		//email e1
		$this->email_became_expert($dbh_r, $member);
		$this->update_approval_status($dbh_w, $member, 5);

	}else if($expert_approval == 6 && $last_expert_approval_type != 6){
    //email e2
		$this->email_expert_application_failed($dbh_r, $member);
		$this->update_approval_status($dbh_w, $member, 6);		
	}
		
	AppendFile('/home/itrial/logs/email/member_status.log', "mid: $member, msg: $msg");

    //	db_do($dbh_w, 'update member_imore set last_expert_approval_type=5 where id=?', $member);
 }
    


}//end class

$webapp = new backend_crontab_member_email_notification(
   array('PARAMS' => array_merge($site_global_config, $local_config))
         );
$webapp->run();

/* Custom Helper Function Implementation start here */

?>

